package model

import (
	"context"
	"fmt"

	"github.com/zeromicro/go-zero/core/stores/cache"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

var _ UserModel = (*customUserModel)(nil)

type (
	// UserModel is an interface to be customized, add more methods here,
	// and implement the added methods in customUserModel.
	UserModel interface {
		userModel
		List(ctx context.Context, page int, limit int) ([]*User, error)
	}

	customUserModel struct {
		*defaultUserModel
	}
)

// NewUserModel returns a model for the database table.
func NewUserModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) UserModel {
	return &customUserModel{
		defaultUserModel: newUserModel(conn, c, opts...),
	}
}

func (c *customUserModel) List(ctx context.Context, page int, limit int) ([]*User, error) {
	query := fmt.Sprintf("select * from %s limit %d OFFSET %d", c.table, limit, limit*page)
	var resp []*User
	// err := c.QueryRow(&resp, "xx", func(conn sqlx.SqlConn, v any) error {
	// 	return conn.QueryRow(v, query)
	// })
	err := c.CachedConn.QueryRowsNoCacheCtx(ctx, &resp, query)
	//	c.CachedConn.QueryRowCtx()
	return resp, err
}
